package com.xhs.zdl.demo.addressChoose;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xhs.zdl.demo.MyUtils;
import com.xhs.zdl.demo.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class AreaFragment extends Fragment {

    private static final String ARG_PARAM1 = "data";
    private static final String ARG_PARAM2 = "level";

    private ArrayList<String> data;//传递过来的数据源
    private OnFragmentInteractionListener mListener;

    private View view;
    private ListView listview;
    private Context context;
    private AreaAdapter adapter;
    private int level;


    public AreaFragment() {
        // Required empty public constructor
    }

    public static AreaFragment newInstance(ArrayList<String> list, int level) {
        AreaFragment fragment = new AreaFragment();
        Bundle args = new Bundle();
        args.putStringArrayList(ARG_PARAM1, list);
        args.putInt(ARG_PARAM2, level);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            //
            data = getArguments().getStringArrayList(ARG_PARAM1);
            level = getArguments().getInt(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_area, container, false);

        findView();
        initView();

        return view;
    }

    private void initView() {
        adapter = new AreaAdapter(context, data);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(onItemClickListener);

    }

    private void findView() {
        listview = (ListView) view.findViewById(R.id.area_listview);
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //单击的时候需要处理地区点击事件，统一交给Activity处理
            String key = (String) adapterView.getAdapter().getItem(i);
            if (null == key) return;
            if (mListener!=null){
                mListener.onFragmentInteraction(key, level);
                MyUtils.Log(level+"");
            }
        }
    };

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    //用来和Activity交互的回调接口
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String key, int level);
    }
}
